<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-简易轮播图</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        body{
            user-select: none;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background: radial-gradient(rgb(211, 212, 192), rgb(221, 150, 42))
        }
        /* 16:9 */
        .container{
            width: 640px;
            height: 360px;
            position:relative;
        }

        img{
            width: 100%;
        }

        .left{
            width: 60px;
            height: 60px;
            border-radius: 50%;
            background: white;
            position:absolute;
            left: 20px;
            top: 160px;
            text-align: center;
            line-height: 60px;
            font-size: 22px;
            font-weight: bold;
            cursor:pointer
        }

        .right{
            cursor:pointer;
            width: 60px;
            height: 60px;
            border-radius: 50%;
            background: white;
            position:absolute;
            right: 20px;
            top: 160px;
            text-align: center;
            line-height: 60px;
            font-size: 22px;
            font-weight: bold;
        }

    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>

    <script type="text/babel">
        let renderCounter = 0;

        class App extends React.Component{
            //2. 准备数据
            state = {
                //所有的图片资源
                images: [
                    './images/1.jpg',
                    './images/2.jpg',
                    './images/3.jpg',
                    './images/4.jpg',
                    './images/5.jpg'
                ],
                //当前正在显示的图片索引
                current: 0
            }

            //1. 返回虚拟 DOM 对象    
            render(){
                console.log(`---render方法的执行次数 ${++renderCounter}----`);
                //获取当前正在显示的图片路径
                let path = this.state.images[this.state.current];
                //3. 将图片路径设置在 src 中, 并为两个按钮绑定事件
                return <div className="container">
                    <img src={path} alt="" />
                    <div className="left" onClick={this.prev}>&lt;</div>
                    <div className="right" onClick={this.next}>&gt;</div>
                </div>
            }

            //4. 声明方法
            prev = () => {
                //获取当前 current 的值
                let {current, images} = this.state;
                //下标自减
                current = current - 1;
                //判断越界
                if(current < 0) current = images.length - 1;
                this.setState({
                    current
                })
            }

            next = () => {
                //获取当前 current 的值
                let {current, images} = this.state;
                //下标自增
                current = current + 1;
                //判断
                if(current >= images.length) current = 0;
                //重新修改 current 的值
                this.setState({
                    current: current
                })
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>